use actix_web::{get, web::{Query, ServiceConfig}, Scope};
use serde::Deserialize;

use crate::{http::{self, IpInfo}, server::web::codec::{new_ok, CborRet}};

#[derive(Debug, Deserialize)]
struct QueryIpReq {
    ip: Option<String>,
}

#[get("/ip_info")]
async fn on_query_ip_info(req: Query<QueryIpReq>) -> CborRet<IpInfo> {
    let info = if let Some(ip) = req.ip.as_ref() {
        http::get_ip_info(ip).await?
    } else {
        http::get_ip_info("").await?
    };
    new_ok(info)
}

pub fn register(cfg: &mut ServiceConfig) {
    let scope = Scope::new("utils")
        .service(on_query_ip_info)
        ;
    cfg.service(scope);
}
